package com.golden.procedure.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.procedure.client.DicClientAPI;
import com.golden.procedure.clientDO.DictionariesDO;
import com.golden.procedure.dao.WorkCommissionMapper;
import com.golden.procedure.domain.WorkCommissionDO;
import com.golden.procedure.domain.WorkerDO;
import com.golden.procedure.entity.WorkCommission;
import com.golden.procedure.entity.WorkCommissionExample;
import com.golden.procedure.query.WorkCommissionQueryDO;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.ResultSupport;
import com.golden.procedure.service.IWorkCommissionService;
import com.golden.procedure.service.IWorkerService;
import com.golden.procedure.util.BeanUtilsExtends;
import com.golden.procedure.util.BonConstant;

/**
 * 工单拍摄服务 <b>Description:</b><br>
 * 
 * @author Reading.Reagan</a>
 * @version 1.0
 * @Note <b>ProjectName:</b> procedure <br>
 *       <b>PackageName:</b> com.golden.procedure.service.impl <br>
 *       <b>ClassName:</b> WorkOrderYYService <br>
 *       <b>Date:</b> 2018年9月21日 上午11:43:45
 */
@Service("workCommissionService")
public class WorkCommissionService extends BaseService implements IWorkCommissionService {

	@Autowired
	DicClientAPI dicClientAPI;

	@Autowired
	WorkCommissionMapper workCommissionMapper;

	@Autowired
	IWorkerService workerService;

	@Override
	public ResultDO add(WorkCommissionDO domain) {
		// 添加员工提成记录；
		WorkCommission entity = new WorkCommission();
		ResultDO resultDO = BeanUtilsExtends.copy(entity, domain);
		if (!resultDO.isSuccess()) {
			return resultDO;
		}
		// 查询并填充服务人员信息
		resultDO = workerService.getWorker(domain.getWorkerID());
		if (!resultDO.isSuccess()) {
			return resultDO;
		}
		WorkerDO workerDO = (WorkerDO) resultDO.getModel(ResultDO.FIRST_MODEL_KEY);
		entity.setEmployeeID(workerDO.getEmployeeID());
		entity.setEmployeeName(workerDO.getEmployeeName());
		entity.setRole(workerDO.getRole());

		entity.setIsDelete(BonConstant.DB_NORMAL);
		entity.setCommissionDate(Calendar.getInstance().getTime());
		entity.setCreatetime(Calendar.getInstance().getTime());
		int ret = workCommissionMapper.insert(entity);
		if (ret < 1) {
			resultDO.setSuccess(false);
			resultDO.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			resultDO.setErrorMsg("员工提成记录添加失败");
		}
		return resultDO;
	}

	@Override
	public ResultDO edit(WorkCommissionDO domain) {
		// 编辑员工提成记录
		WorkCommission entity = new WorkCommission();
		ResultDO resultDO = BeanUtilsExtends.copy(entity, domain);
		if (!resultDO.isSuccess()) {
			return resultDO;
		}
		entity.setModifitime(Calendar.getInstance().getTime());
		// 通过id更新
		int ret = workCommissionMapper.updateByPrimaryKeySelective(entity);
		if (ret < 1) {
			resultDO.setSuccess(false);
			resultDO.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			resultDO.setErrorMsg("拍摄表数据更新失败");
		}
		return resultDO;
	}

	/**
	 * 查询员工提成记录列表 <b>Description:</b><br>
	 * 
	 * @param queryDO
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月19日 下午4:10:05 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO query(WorkCommissionQueryDO queryDO) {
		ResultDO result = new ResultSupport();

		// 1.查询席位
		if (isNumberInValid(queryDO.getWorkOrderID())) {
			// 席位查询失败
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("工单参数错误");
			result.setSuccess(false);
			return result;
		}

		WorkCommissionExample example = new WorkCommissionExample();
		WorkCommissionExample.Criteria criteria = example.createCriteria();
		criteria.andWorkOrderIDEqualTo(queryDO.getWorkOrderID());
		List<WorkCommission> list = workCommissionMapper.selectByExample(example);

		List<WorkCommissionDO> doList = getWorkCommissionDOList(list);
		result.setModel(ResultDO.FIRST_MODEL_KEY, doList);

		return result;
	}

	public WorkCommissionDO getWorkCommissionDO(WorkCommission obj) {
		if (obj == null)
			return null;
		WorkCommissionDO dst = new WorkCommissionDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setModifitime(obj.getModifitime().getTime() + "");
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		// TODO: 调用字典（open)
		if (!isNumberInValid(dst.getCommissionProj())) {
			DictionariesDO dicDO = dicClientAPI.get(dst.getCommissionProj());
			dst.setCommissionProjName(dicDO != null ? dicDO.getDataItem() : "");
		}
		if (!isNumberInValid(dst.getCommissionItem())) {
			DictionariesDO dicDO = dicClientAPI.get(dst.getCommissionItem());
			dst.setCommissionName(dicDO != null ? dicDO.getDataItem() : "");
		}
		// test data
		// dst.setCommissionProjName("上门拍摄");
		// dst.setCommissionName("提成业绩比例%");

		if (null != obj.getCommissionDate()) {
			dst.setCommissionDateStr(obj.getCommissionDate().getTime() + "");
		}
		return dst;
	}

	public List<WorkCommissionDO> getWorkCommissionDOList(List<WorkCommission> list) {
		List<WorkCommissionDO> doList = new ArrayList<WorkCommissionDO>();
		if (null != list && list.size() > 0) {
			for (WorkCommission data : list) {
				WorkCommissionDO dst = getWorkCommissionDO(data);
				doList.add(dst);
			}
		}
		return doList;
	}
}
